package com.example.demo.aop;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

@Component
@Aspect
public class AopLogConfig {


    /**
     * 定义切点
     * * com.example.demo.service.*.*(..))
     */
    @Pointcut("execution(* com.*..*.service.*.add(..))")
    public void addpointcut() {
    }

    @Before("addpointcut()")
    public void before() {
        System.out.println("---before---");
    }

    @After("addpointcut()")
    public void after() {
        System.out.println("---after---");
    }

// This annotation indicates that the method below should be executed after a method execution
// that matches the pointcut expression defined in the addpointcut() method.
    @AfterReturning("addpointcut()")
    public void afterReturning() {
    // This line prints the string "---afterReturning---" to the standard output.
    // It serves as a log message indicating that the afterReturning advice has been executed.
        System.out.println("---afterReturning---");
    }
    @AfterThrowing("addpointcut()")
    public void afterThrowing() {
        System.out.println("---afterThrowing---");
    }

    @Around("addpointcut()")
    public Object around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        Object o =  null;
        try {
            System.out.println("---around before---");
            o = proceedingJoinPoint.proceed();
            System.out.println("---around after---");
        }  catch (Exception e) {
            System.out.println("---around catch---");
        } finally {
            System.out.println("---around finally---");
        }
        return o;
    }
}
